﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.FrameWork;

namespace uswinweb.admin
{
    /// <summary>
    /// 企业管理员调整与权限设置
    /// </summary>
    public partial class pop_corpmanager_set : AdminPage
    {
        protected DataTable maindt = new DataTable();
        protected DataTable subdt = new DataTable();
        protected CompanyInfo cinfo = new CompanyInfo();
        protected UserInfo minfo = new UserInfo();
        protected int compid = SASRequest.GetInt("cid", 0);
        protected int managerid = SASRequest.GetInt("uid", 0);
        protected bool createAdmin = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            maindt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.MainMenu);
            subdt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.SubMenu);
            if (compid <= 0 && managerid <= 0)
            {
            }
            else
            {
                cinfo = Companys.GetCompanyByID(compid);
                if (managerid > 0) minfo = Users.GetUser(managerid);
                else minfo = Users.GetUser(cinfo.Manager);
            }
            if (compid > 0 && managerid <= 0) createAdmin = true;

            if (SASRequest.IsPost())
            {
                SetManagerAuth();
            }
        }

        /// <summary>
        /// 设置企业管理员权限
        /// </summary>
        private void SetManagerAuth()
        {
            string auths = "";
             int allcount = 0;
             foreach (System.Data.DataRow dr in maindt.Rows)
             {
                 int subcount = 0;
                 foreach (System.Data.DataRow srow in subdt.Select("parentid=" + dr["menuid"]))
                 {
                     string authstr = "";
                     authstr += SASRequest.GetString("auth_main_" + allcount) + ",";
                     if (authstr.Trim(',') == "")
                     {
                         allcount++;
                         continue;
                     }

                     authstr = authstr + SASRequest.GetString("auth_" + allcount);
                     authstr = authstr.Trim(',').Replace(',', '|');

                     if (authstr != "") authstr += "|" + srow["frameid"] + ",";
                     allcount++;
                     auths += authstr;
                 }
             }

            if (!Users.SetCorpManagerAuth(SASRequest.GetInt("comp_hidd_luffy", 0), SASRequest.GetInt("manager_hidd_luffy", 0), createAdmin, auths))
            {
                base.RegisterStartupScript("PAGETEMPLATE", "权限设置出错，请与管理员联系！");
                return;
            }

            base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
        }
    }
}